using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.ViewModels.CMS;
using FiltronASP.Consts;
using System.Text.RegularExpressions;

namespace FiltronASP.Controllers
{
    public class CMSController : Controller
    {
        private const int DEFAULT_HOME_PAGE_ID = 354;
        private const int POLISH_HOME_PAGE_ID = 71;
        private const int UKRAINIAN_HOME_PAGE_ID = 1921;
        private const int RUSSIAN_HOME_PAGE_ID = 1694;
        private const int GERMAN_HOME_PAGE_ID = 998;
        private const int FRENCH_HOME_PAGE_ID = 1346;
        private const int SPANISH_HOME_PAGE_ID = 824;
        private const int ITALIAN_HOME_PAGE_ID = 1520;
        private const int HUNGARIAN_HOME_PAGE_ID = 650;

        /// <summary>
        /// Metoda odpowiedzialna za wyswietlanie strony na podstawie contentu w bazie.
        /// </summary>
        /// <param name="pageId">id strony</param>
        /// <returns>odpowiedni widok</returns>
        [OutputCache(Duration = 18000, VaryByParam = "pageId;nocache", VaryByHeader = "host", VaryByCustom = "browserLang")]
        public ActionResult Index(int? pageId, string pageAlias)
        {
            if (pageId == -1)
            {
                pageId = DEFAULT_HOME_PAGE_ID;
                string host = Request.Url.Host;
                if (!String.IsNullOrEmpty(host))
                {
                    if ((host.ToLower().Equals(GlobalConsts.WIX_UA_DOMAIN.ToLower())) || (host.ToLower().Equals(GlobalConsts.WWW_WIX_UA_DOMAIN.ToLower())))
                    {
                        pageId = GlobalConsts.UKRAINIAN_HOME_PAGE_ID;
                    }   //if
                    else if (Request.UserLanguages != null)
                    {
                        string lang = Request.UserLanguages[0];
                        if (lang != null)
                        {
                            if (!lang.StartsWith("tr"))
                            {
                                if (lang.Length > 2)
                                {
                                    lang = lang.Substring(0, 2);
                                }   //if
                                pageId = this.GetPageIdByLangAbbr(lang);
                            }   //if
                        }   //if
                    }   //if
                }   //if                
                else if (Request.UserLanguages != null)
                {
                    string lang = Request.UserLanguages[0];
                    if (lang != null)
                    {
                        if (!lang.StartsWith("tr"))
                        {
                            if (lang.Length > 2)
                            {
                                lang = lang.Substring(0, 2);
                            }   //if
                            pageId = this.GetPageIdByLangAbbr(lang);
                        }   //if
                    }   //if
                }   //if
            }   //if
            Page page = this.getCurrentPage(pageId);
            if (page != null)
            {
                if (!page.language_id.HasValue)
                {
                    Response.Redirect(String.Format("http://before.wixeurope.com/{0}", page.page_link));
                }   //if
                if (page.language_id.HasValue)
                {
                    if (!GlobalConsts.NEW_VERSION_LANGUAGES.Contains(page.language_id.Value))
                    {
                        Response.Redirect(String.Format("http://before.wixeurope.com/{0}", page.page_link));
                    }   //if                        
                }   //if
            }   //if
            return this.renderPage(pageId);
        }

        private ViewResult renderPage(int? pageId)
        {
            if (!pageId.HasValue)
            {
                pageId = GlobalConsts.DEFAULT_HOME_PAGE_ID;
            }   //if
            Page currentPage = this.getCurrentPage(pageId);
            if (currentPage == null)
            {
                return View("NoPageFound");
            }   //if null
            string masterPageName = this.GetTemplatePath(currentPage);
            if (masterPageName == null)
            {
                return View("NoTemplateFound");
            }   //if
            return View("Index", masterPageName, this.GetCMSViewModel(currentPage));
        }

        /// <summary>
        /// Metoda pobierajaca biezaca strone.
        /// </summary>
        /// <param name="pageId">id strony</param>
        /// <returns>biezaca strona</returns>
        private Page getCurrentPage(int? pageId)
        {
            if (pageId == null)
            {
                return null;
            }   //if null
            IPageDAO pageDAO = new PageDAO();
            return pageDAO.FindByPK(pageId);
        }

        /// <summary>
        /// Metoda pobierajaca sciezke do szablonu (master page).
        /// </summary>
        /// <param name="page">generowana strona</param>
        /// <returns>sciezka do szablonu</returns>
        private string GetTemplatePath(Page page)
        {
            if (page == null)
            {
                return null;
            }   //if
            ITemplateDAO templateDAO = new TemplateDAO();
            Template template = templateDAO.FindByPK(page.template_id);
            if (template == null)
            {
                return null;
            }   //if null
            return template.template_path;
        }
        
        /// <summary>
        /// Metoda zwracajaca model danych dla widoku.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <returns>model danych</returns>
        private CMSViewModel GetCMSViewModel(Page page)
        {
            return new CMSViewModel(page);
        }

        private int GetPageIdByLangAbbr(string langAbbr)
        {
            if (String.IsNullOrEmpty(langAbbr))
            {
                return GlobalConsts.DEFAULT_HOME_PAGE_ID;
            }   //if
            switch (langAbbr.ToLower())
            {
                case "en":
                    return GlobalConsts.DEFAULT_HOME_PAGE_ID;
                case "pl":
                    return GlobalConsts.POLISH_HOME_PAGE_ID;
                case "uk":
                    return GlobalConsts.UKRAINIAN_HOME_PAGE_ID;
                case "ru":
                    return GlobalConsts.RUSSIAN_HOME_PAGE_ID;
                case "de":
                    return GlobalConsts.GERMAN_HOME_PAGE_ID;
                case "fr":
                    return GlobalConsts.FRENCH_HOME_PAGE_ID;
                case "es":
                    return GlobalConsts.SPANISH_HOME_PAGE_ID;
                case "it":
                    return GlobalConsts.ITALIAN_HOME_PAGE_ID;
                case "hu":
                    return GlobalConsts.HUNGARIAN_HOME_PAGE_ID;
                default:
                    return GlobalConsts.DEFAULT_HOME_PAGE_ID;
            }   //switch
        }

    }
}
